package cn.edu.nuc.crm.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.nuc.crm.dao.ProductMapper;
import cn.edu.nuc.crm.dto.PageInfo;
import cn.edu.nuc.crm.entity.Product;
import cn.edu.nuc.crm.service.interfaces.ProductService;
@Service
public class ProductServiceImpl implements ProductService {
	@Autowired
	private ProductMapper productMapper;
	
	
	/**
	 * 商品列表
	 */
	@Override
	public List<Product> product() {
		List<Product> product = new ArrayList<Product>();
		product = productMapper.product();
		return product;
	}

	@Override
	public PageInfo pi(int current) {
		PageInfo pi = new PageInfo(current);
		int count = productMapper.count();
		pi.setCount(count);
		List<Product> findByid=productMapper.findByid(pi.getStart(), pi.getOffset());
		System.out.println("总的记录数:"+count+"--"+pi);
		pi.setList(findByid);
		return pi;
	}

	@Override
	public List<Product> selectByName(String productname) {
		List<Product> result=productMapper.selectByName(productname);
		return result;
	}

	@Override
	public int insert(Product product) {	
		int count=productMapper.insert(product);
		if(count>0){
			return count;
		}else{
			throw new RuntimeException("增加失败");
		}
	}

	@Override
	public int deleteByPrimaryKey(Integer productid) {
		int result=productMapper.deleteByPrimaryKey(productid);
		return result;
	}
	
	@Override
	public Product selectByPrimaryKey(int productid) {
		Product result=productMapper.selectByPrimaryKey(productid);
		return result;
	}

	@Transactional
	public int updateByPrimaryKey(Product product) {
		int result=productMapper.updateByPrimaryKey(product);
		return result;
	}

	@Override
	public Product find(int productid) {
		Product result=productMapper.find(productid);
		return result;
	}

}
